package com.xxxx.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.exception.GlobalException;
import com.xxxx.seckill.mapper.UserMapper;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IUserService;
import com.xxxx.seckill.utils.CookieUtil;
import com.xxxx.seckill.utils.MD5Util;
import com.xxxx.seckill.utils.UUIDUtil;
import com.xxxx.seckill.vo.LoginVo;
import com.xxxx.seckill.vo.RespBean;
import com.xxxx.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author heyanfeng
 * @since 2022-07-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author heyanfeng
     * @Description // 登录
     * @Date 11:03 2022/8/1
     * @Param [loginVo, request, response]
     * @return com.xxxx.seckill.vo.RespBean
     **/
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //利用validator组件，仿照 @NotNull方法改写，自定义的手机号校验的注解实现手机号的验证。
//        //判断手机号码是否为空
//        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
//            return RespBean. error(RespBeanEnum.LOGIN_ERROR);
//        }
//        //手机号验证
//        if ( !ValidatorUtil.isMobile (mobile)) {
//            return RespBean. error(RespBeanEnum.MOBILE_ERROR);
//        }

        //根据手机号获取用户
        User user = userMapper.selectById (mobile);
        if (null == user){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        System.out.println("校验密码："+MD5Util.formPassToDBPass(password, user.getSalt()));
        //判断密码是否正确
        if (!MD5Util.formPassToDBPass(password , user.getSalt()).equals(user.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();
        //将用户信息存入redis中
        redisTemplate.opsForValue().set("user:"+ticket,user);
       // request.getSession().setAttribute(ticket,user);
        CookieUtil.setCookie(request,response, "userTicket" ,ticket);

        return RespBean.success(ticket);
    }

    /**
     * @Author heyanfeng
     * @Description //  根据cookie获取用户
     * @Date 11:59 2022/8/1
     * @Param [userTicket]
     * @return com.xxxx.seckill.pojo.User
     **/
    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)) {
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get( "user:" + userTicket);
        if (user !=null){
            CookieUtil.setCookie(request, response,"userTicket",userTicket);
        }
        return user;
    }

    /**
     * @Author heyanfeng
     * @Description // 更新用户
     * @Date 15:52 2022/8/23
     * @Param [userTicket, id, password]
     * @return com.xxxx.seckill.vo.RespBean
     **/
    @Override
    public RespBean updatePassword(String userTicket, String password,HttpServletRequest request, HttpServletResponse response) {

        User user = getUserByCookie(userTicket,request,response);
        if (user == null) {
            throw new GlobalException(RespBeanEnum. MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSalt()));
        int result = userMapper.updateById(user);
        if (1 == result) {
            // 删除Redis
            redisTemplate.delete("user: " + userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }

}
